<?php
class UsersController extends AppController {
	var $name = 'Users';
	var $layout = 'login';
	

	function beforeFilter() {
		$this->Auth->redirect(array('controller'=>'projects','action'=>'index'));
	}
	function index() {
		//debug($this);
		$this->login();
	}
	
	function login() {
		//$this->layout = 'login';
		if ($this->Auth->user()) {
			//debug('ok');
			/*if (!empty($this->data)) {
                    if (empty($this->data['User']['remember_me'])) {
                        $this->Cookie->del('User');
                    } else {
                        $cookie = array();
                        $cookie['username'] = $this->data['User']['username'];
                        $cookie['password'] = $this->data['User']['password'];
                        $this->Cookie->write('User', $cookie, true, '+2 weeks');
                    }
                unset($this->data['User']['remember_me']);
			}*/
			$this->redirect(array('controller'=>'projects','action'=>'index'));
		} else if (!empty($this->data)){
			$this->Session->setFlash('The username or password you entered is incorrect.');
		}
	}
	
	function logout() {
		$this->redirect($this->Auth->logout());
	}
	
	function installACL() {
		$this->redirect(array('action' => 'createGroupsAROS'), null, true);
	}
	function createGroupsAROS () {
		 $aro =& $this->Acl->Aro;
		 
		 $this->loadModel('TypeUser');
		 
		 $groupUsers = $this->TypeUser->find('all');
		 
		 $groups = array();
		 $i = 0;
		 foreach ($groupUsers as $groupUser) {
		 	$groups[$i] = array ('alias' => $groupUser['TypeUser']['typeUserName']);
		 	$i++;
		 }
		 foreach ($groups as $data) {
		 	$aro->create();
		 	$aro->save($data);
		 }
		$this->redirect(array('action' => 'addUserToGroups'), null, true);
	}
	function addUserToGroups() {
		$aro = new Aro();
		$users = $this->User->find('all');
		$usersForAROS = array();
		$i = 0;
		foreach ($users as $user) {
			$typeUserName = $user['TypeUser']['typeUserName'];
			$usersForAROS[$i] = array (
		 		'alias' => $user['User']['userName'],
		 		'parent_id' => $user['TypeUser']['id'],
		 		'model' => 'User'
		 	);
		 	
		 	$i++;
		}

		foreach ($usersForAROS as $data) {
			$aro->create();
			$aro->save($data);
		}
		
		$this->redirect(array('action' => 'createAcos'), null, true);
	}
	function createAcos() {
		
		$aco = new Aco();
		
		$lists = array (
			0 => array ('alias' => 'schools'),
			1 => array ('alias' => 'sponsors'),
			2 => array ('alias' => 'subcribers'),
			3 => array ('alias' => 'messages'),
			4 => array ('alias' => 'systemReports'),
			5 => array ('alias' => 'systems'),
		);
		foreach ($lists as $data) {
			$aco->create();
			$aco->save($data);
		}
		
		$this->redirect(array('action' => 'assignPermission'), null, true);
	}
	function assignPermission() {
		$this->loadModel('TypeUser');
		$groupUsers = $this->TypeUser->find('all');
		
		$i = 0;
		foreach ($groupUsers as $groupUser) {
			if ($groupUser['TypeUser']['typeUser'] == 1) {
				$user = $groupUser['TypeUser']['typeUserName'];
				
				$this->log($user);
				
				$this->Acl->allow($user,'schools');
				$this->Acl->allow($user,'sponsors');
			} else if ($groupUser['TypeUser']['typeUser'] == 2) {
				$user = $groupUser['TypeUser']['typeUserName'];
				$this->log($user);
				
				$this->Acl->deny($user,'schools');
				$this->Acl->deny($user,'sponsors');
				
				$this->Acl->allow($user,'messages');
				$this->Acl->allow($user,'reports');
			} 
		}
		
		$this->redirect(array('action' => 'installed'), null, true);
	}
	
	function installed() {
		
	}
	
}
?>